<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="chinese">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="1. Spring 是如何实现的以主流的 JDK 版本 1.8 来说，String 内部实际存储结构为 char 数组 2. Spring 有哪些重要的方法 多构造方法  12345678910111213141516171819// String 为参数的构造方法public String(String original) &amp;#123;    this.value = original.valu">
<meta name="keywords" content="Java Php hexo Thinking in Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java String 总结">
<meta property="og:url" content="http://yoursite.com/2020/03/23/Java-String-总结/index.html">
<meta property="og:site_name" content="190 muscle coder">
<meta property="og:description" content="1. Spring 是如何实现的以主流的 JDK 版本 1.8 来说，String 内部实际存储结构为 char 数组 2. Spring 有哪些重要的方法 多构造方法  12345678910111213141516171819// String 为参数的构造方法public String(String original) &amp;#123;    this.value = original.valu">
<meta property="og:locale" content="chinese">
<meta property="og:image" content="http://yoursite.com/images/string.png">
<meta property="og:image" content="http://yoursite.com/images/stringjvm.png">
<meta property="og:updated_time" content="2020-03-23T08:22:55.745Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java String 总结">
<meta name="twitter:description" content="1. Spring 是如何实现的以主流的 JDK 版本 1.8 来说，String 内部实际存储结构为 char 数组 2. Spring 有哪些重要的方法 多构造方法  12345678910111213141516171819// String 为参数的构造方法public String(String original) &amp;#123;    this.value = original.valu">
<meta name="twitter:image" content="http://yoursite.com/images/string.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":14,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '190coder'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2020/03/23/Java-String-总结/">





  <title>Java String 总结 | 190 muscle coder</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="chinese">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">190 muscle coder</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/03/23/Java-String-总结/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="190coder">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="190 muscle coder">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Java String 总结</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-03-23T12:09:17+08:00">
                2020-03-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Java-基础/" itemprop="url" rel="index">
                    <span itemprop="name">Java 基础</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-Spring-是如何实现的"><a href="#1-Spring-是如何实现的" class="headerlink" title="1. Spring 是如何实现的"></a>1. Spring 是如何实现的</h2><p>以主流的 JDK 版本 1.8 来说，String 内部实际存储结构为 char 数组</p>
<h2 id="2-Spring-有哪些重要的方法"><a href="#2-Spring-有哪些重要的方法" class="headerlink" title="2. Spring 有哪些重要的方法"></a>2. Spring 有哪些重要的方法</h2><ul>
<li>多构造方法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">// String 为参数的构造方法</span><br><span class="line">public String(String original) &#123;</span><br><span class="line">    this.value = original.value;</span><br><span class="line">    this.hash = original.hash;</span><br><span class="line">&#125;</span><br><span class="line">// char[] 为参数构造方法</span><br><span class="line">public String(char value[]) &#123;</span><br><span class="line">    this.value = Arrays.copyOf(value, value.length);</span><br><span class="line">&#125;</span><br><span class="line">// StringBuffer 为参数的构造方法</span><br><span class="line">public String(StringBuffer buffer) &#123;</span><br><span class="line">    synchronized(buffer) &#123;</span><br><span class="line">        this.value = Arrays.copyOf(buffer.getValue(), buffer.length());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">// StringBuilder 为参数的构造方法</span><br><span class="line">public String(StringBuilder builder) &#123;</span><br><span class="line">    this.value = Arrays.copyOf(builder.getValue(), builder.length());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>不过  StringBuffer 和 StringBuilder一般为单独使用</p>
<ul>
<li><p>equals() 比较两个字符串是否相等。<br>String类型重写了Object中的equals()方法，equals()方法需要传递一个Object类型的参数值，在比较时会先通过instanceof判断是否为String，之后循环比较char数组。返回 boolure。</p>
</li>
<li><p>compareTo() 比较两个字符串，返回 int 型。</p>
</li>
<li><p>其他重要方法：</p>
<ul>
<li>indexOf()：查询字符串首次出现的下标位置</li>
<li>lastIndexOf()：查询字符串最后出现的下标位置</li>
<li>contains()：查询字符串中是否包含另一个字符串</li>
<li>toLowerCase()：把字符串全部转换成小写</li>
<li>toUpperCase()：把字符串全部转换成大写</li>
<li>length()：查询字符串的长度</li>
<li>trim()：去掉字符串首尾空格</li>
<li>replace() 替换掉字符串中的某些字符</li>
<li>split()：把字符串分割并返回字符串数组 </li>
<li>join()：把字符串数组转为字符串</li>
</ul>
</li>
</ul>
<h2 id="3-和-equals-的区别"><a href="#3-和-equals-的区别" class="headerlink" title="3. == 和 equals 的区别"></a>3. == 和 equals 的区别</h2><p>== 是引用的内存地址的比较，equals是值比较。Object equals 是直接比较引用地址。String 重写了equals 方法，先比较引用地址，然后比较类型是不是String，最后把2个字符串转换成Char 数组对比，循环每一个字符，其中有一个不想等就返回false，否则继续对比。</p>
<h2 id="4-final修饰的好处"><a href="#4-final修饰的好处" class="headerlink" title="4. final修饰的好处"></a>4. final修饰的好处</h2><ul>
<li>安全：如果有一些列校验后，如果是可变的，校验后他的内部值又被改变了，可能引起系统崩溃问题。</li>
<li>效率高：能够缓存结果（常量池）如果是可变的话，则有可能重写拷贝一个新值进行传参，性能空间都有浪费。</li>
</ul>
<p><img src="/images/string.png" alt></p>
<p>如果 String 是可变的，那当 s1 的值修改之后，s2 的值也跟着改变了，这样就和我们预期的结果不相符了，因此也就没有办法实现字符串常量池的功能了。</p>
<h2 id="5-String-和-StringBuilder、StringBuffer-的区别"><a href="#5-String-和-StringBuilder、StringBuffer-的区别" class="headerlink" title="5. String 和 StringBuilder、StringBuffer 的区别"></a>5. String 和 StringBuilder、StringBuffer 的区别</h2><ul>
<li>String 类型不可变，所以拼接字符串性能会很低。</li>
<li>StringBuffer synchronized 线程安全</li>
<li>StringBuilder 线程不安全，但效率高。非并发都用StringBuilder。</li>
</ul>
<h2 id="6-String-和-JVM"><a href="#6-String-和-JVM" class="headerlink" title="6. String 和 JVM"></a>6. String 和 JVM</h2><p>String常见的创建方式有两种，直接赋值的方式 “String s1 = “Java”;”和“String s2 = newString(“Java”);”的方式，但两者在JVM的存储区域却截不同，在JDK1.8之后，s1先在常量池中找字符串“java”，如果有相同的字符则直接返回常量句柄。s2直接在堆中创建，需要调用intern 方法才能把此字符串保存到常量池中。<br><img src="/images/stringjvm.png" alt></p>
<p>DK 1.7 之后把永生代换成的元空间，把字符串常量池从方法区移到了 Java 堆上。</p>
<p>除此之外编译器还会对 String 字符串做一些优化，例如以下代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">String s1 = &quot;Ja&quot; + &quot;va&quot;;</span><br><span class="line">String s2 = &quot;Java&quot;;</span><br><span class="line">System.out.println(s1 == s2);// true</span><br></pre></td></tr></table></figure>

<p>从编译代码看，代码 “Ja”+”va” 被直接编译成了 “Java” ，因此 s1==s2 的结果才是 true，这就是编译器对字符串优化的结果。</p>
<h2 id="7-总结"><a href="#7-总结" class="headerlink" title="7. 总结"></a>7. 总结</h2><p>String 构造方法，常用方法，重点是 equals() 、 CompareTo() ,final的好处，string在jvm执行的细节。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/03/23/Spring-Cloud-Sleuth/" rel="next" title="Spring Cloud Sleuth">
                <i class="fa fa-chevron-left"></i> Spring Cloud Sleuth
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/03/23/Java-HashMap-总结/" rel="prev" title="Java HashMap 总结">
                Java HashMap 总结 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="190coder">
            
              <p class="site-author-name" itemprop="name">190coder</p>
              <p class="site-description motion-element" itemprop="description">打铁还需自身硬</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">35</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-Spring-是如何实现的"><span class="nav-number">1.</span> <span class="nav-text">1. Spring 是如何实现的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-Spring-有哪些重要的方法"><span class="nav-number">2.</span> <span class="nav-text">2. Spring 有哪些重要的方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-和-equals-的区别"><span class="nav-number">3.</span> <span class="nav-text">3. == 和 equals 的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-final修饰的好处"><span class="nav-number">4.</span> <span class="nav-text">4. final修饰的好处</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-String-和-StringBuilder、StringBuffer-的区别"><span class="nav-number">5.</span> <span class="nav-text">5. String 和 StringBuilder、StringBuffer 的区别</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-String-和-JVM"><span class="nav-number">6.</span> <span class="nav-text">6. String 和 JVM</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-总结"><span class="nav-number">7.</span> <span class="nav-text">7. 总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">190coder</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>





<div>
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>
    <span id="busuanzi_container_site_uv">本站访客数<span id="busuanzi_value_site_uv"></span>人</span>
</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
